﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using IAC_2013.com.iac.util;
using IAC_2013.com.iac.form;
using DevComponents.DotNetBar.Controls;

namespace IAC_2013.com.iac.action
{
    class CurrentcyAction
    {
        SqlConnection conn = new SqlConnection(DBUtils.CONNECTION_STRING);
        SqlCommand cmd;
        SqlDataAdapter da;
        DataTable tb;
        public Boolean addCurrentcy(CurrentcyForm frm)
        {
            string sql = "INSERT INTO lt_loai_tien VALUES(";
            sql += "'"+ frm.currentcyCode +"', ";
            sql += "N'"+ frm.currentcyName +"', ";
            sql += "N'"+ frm.description +"'";
            sql += ")";

            try
            {
                cmd = new SqlCommand(sql, conn);
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
                cmd.Dispose();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public Boolean editCurrentcy(CurrentcyForm frm)
        {
            string sql = "update lt_loai_tien set ma_lt = '"+ frm.currentcyCode +"', ten_lt = N'"+ frm.currentcyName +"', mo_ta = N'"+ frm.description +"' where id_lt = " + frm.currentcyId;

            try
            {
                cmd = new SqlCommand(sql, conn);
                conn.Open();
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                conn.Close();
                return true;
            }
            catch (Exception)
            {
                return false;
            }

        }

        public Boolean deleteCurrentcy(CurrentcyForm frm)
        {
            string sql = "delete from lt_loai_tien where id_lt = '"+ frm.currentcyId +"'";

            try
            {
                cmd = new SqlCommand(sql, conn);
                conn.Open();
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                conn.Close();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public DataTable listCurrentcy(CurrentcyForm frm)
        {
            string sql = "select * from lt_loai_tien lt where 1 = 1 ";
            if (frm.currentcyCode != null && frm.currentcyCode != "")
            {
                sql += " and LOWER(lt.ma_lt) LIKE '"+ "%" + frm.currentcyCode + "%" + "'";
            }
            if (frm.currentcyName != null && frm.currentcyName != "")
            {
                sql += " and LOWER(lt.ten_lt) LIKE '" + "%" + frm.currentcyName + "%" + "'";
            }

            cmd = new SqlCommand(sql, conn);
            conn.Open();
            da = new SqlDataAdapter(cmd);
            tb = new DataTable();
            da.Fill(tb);
            cmd.Dispose();
            conn.Close();
            return tb;
        }
    }
}
